package me.zhengjie.modules.member.rest;

import com.qiaofang.applet.service.member.service.MemberExploreService;
import com.qiaofang.applet.service.member.service.dto.MemberBrowseListQueryDto;
import com.qiaofang.applet.service.member.service.dto.MemberExploreDetailDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by IDEA.
 *
 * @author QF2019
 * @company qiaofang
 * @date 2020/9/12 11:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "数据管理：用户浏览记录")
@RequestMapping("/api/memberBrowse")
public class MemberBrowseController {

    @Autowired
    private MemberExploreService memberExploreService;

    @Autowired
    private UserService userService;

    @GetMapping
    @ApiOperation(value = "查询用户浏览记录列表", notes = "查询用户浏览记录列表")
    public ResponseEntity<Page<MemberExploreDetailDto>> list(MemberBrowseListQueryDto memberBrowseListQueryDto, Pageable pageable) {
        memberBrowseListQueryDto.setCorporationCode(SecurityUtils.getCorporationCode());
        memberBrowseListQueryDto.setCityCodeList(userService.getCityCodeList(SecurityUtils.getCurrentUsername()));
        return ResponseEntity.ok(memberExploreService.listAllMemberExplore(memberBrowseListQueryDto, pageable));
    }

//    @PostMapping("/exportExcel")
//    @ApiOperation(value = "导出用户浏览记录", notes = "导出用户浏览记录")
//    public void exportExcel(@RequestBody DataRequest<MemberBrowseListQueryDto> dataRequest, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
//        List<MemberBrowse> list = memberBrowseService.getExportList(dataRequest.getParam());
//        if (CollectionUtils.isEmpty(list)) {
//            return;
//        }
//        List<MemberBrowseExcelVo> excelList = new LinkedList<>();
//        for (MemberBrowse memberBrowse : list) {
//            MemberBrowseExcelVo memberBrowseExcelVo = new MemberBrowseExcelVo();
//            BeanUtils.copyProperties(memberBrowse, memberBrowseExcelVo);
//            excelList.add(memberBrowseExcelVo);
//        }
//        String formatDate = DateUtil.localDateTimeFormatyMd(LocalDateTime.now());
//        String name = "用户浏览记录-" + formatDate + ".xls";
//        String userAgent = request.getHeader("User-Agent");
//        byte[] bytes = userAgent.contains("MSIE") ? name.getBytes() : name.getBytes("UTF-8"); // name.getBytes("UTF-8")处理safari的乱码问题  
//        name = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码  
//        response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", name));
//        response.setHeader("Content-Disposition", "attachment;filename=" + name);
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("application/OCTET-STREAM;charset=UTF-8");
//        ExcelUtil<MemberBrowseExcelVo> util = new ExcelUtil<>(MemberBrowseExcelVo.class);
//        try {
//            util.exportExcel(excelList, "用户浏览记录", 65536, response.getOutputStream());
//        } catch (IOException e) {
//            LOGGER.info("导出浏览记录失败", e);
//        }
//    }
}